<?php
/*
Plugin Name: WP Smiley Switcher
Plugin URI: http://www.web-templates.nu/2008/09/13/wp-smiley-switcherwp-smiley-switcher/
Description: Set your own smiley URL or choose from smiley packs to activate.
Version: 0.2 (edited by Seoul)
Author: Jens T&ouml;rnell
Author URI: http://www.web-templates.nu
*/

function wp_smiley_switcher_submenu() {
    add_submenu_page('options-general.php', 'Smiley Switcher', 'Smiley Switcher', 10, __FILE__, 'wp_smiley_switcher_add_menu');
}

function wp_smiley_switcher_add_menu()
{
	if(isset($_POST['submitter']))
	{
		if($_POST["enableposts"] == "")
		{
			$save_enableposts = "off";
		}
		else
		{
			$save_enableposts == "on";
		}
		if($_POST["enablecomments"] == "")
		{
			$save_enablecomments = "off";
		}
		else
		{
			$save_enablecomments == "on";
		}
		
		$settings = array (
		"dir"				=> $_POST["smiledir"],
		"choice"			=> $_POST["smilechoice"],
		"enableposts"		=> $save_enableposts,
		"enablecomments"	=> $save_enablecomments,
		);
		update_option('wp_smiley_switcher', $settings);
	}
	
	$settings = get_option('wp_smiley_switcher');
	$enableposts = $settings["enableposts"];
	$enablecomments = $settings["enablecomments"];
	$dir = $settings["dir"];
	$choice = $settings["choice"];
	$fulldir = get_bloginfo('wpurl').'/wp-content/plugins/wp-smiley-switcher/'.$dir."/";
	$path = get_bloginfo('wpurl')."/wp-content/plugins/wp-smiley-switcher/";
	
	$smileys_array = get_array();
	//$smileys_short_array = wp_smiley_switcher_short_array();
	
	//echo "Comments->$enablecomments." && "posts->";
	//echo $enableposts;
	
	?>
	
	<div class="wrap">
	<h2>Smiley Switcher options</h2>
	<form method="post" name="options" target="_self">
	<h3 style="font-weight: normal;">Current smiley URL:</h3>
	<p><?php echo get_bloginfo('wpurl') ?>/wp-content/plugins/wp-smiley-switcher/<strong><?php
	if($choice != "default")
	{
		echo $choice;
	}
	else
	{
		echo $dir;
	}
	?></strong></p>
	<table class="form-table" width="100%" border="0">
	<tr>
		<th scope='row'>
			<?php
				echo "<input type='radio' name='smilechoice' value='default'";
				if($choice == "default")
				{
					echo " checked='checked'";
				}
				echo "/>&nbsp;&nbsp;Smiley directory:";
			?>
		</th>
		<th><input type='text' name='smiledir' value="<?php echo $dir; ?>" /></th>
		
		<td><?php echo get_bloginfo('wpurl'); ?>/wp-content/plugins/wp-smiley-switcher/<strong><?php echo $dir; ?></strong></td>
		</tr>
	
		<tr>
		<th scope='row'><input type='radio' name='smilechoice' value='yellowpack' <?php if($choice == "yellowpack" || $choice == "") { echo " checked='checked'"; } ?> />&nbsp;&nbsp;<a href='http://www.jenst.se/2007/09/02/wordpress-smilies-icon-pack-free-download'>Yellowpack</a>&nbsp;-&nbsp;By&nbsp;<a href='http://www.jenst.se'>Jens&nbsp;T&ouml;rnell</a>
		</th>
	
		<th scope='row' colspan="2">
			<?php for($i=0; $i<sizeof($smileys_short_array); $i++)
			{
				echo "<img src='". $path. "yellowpack/" .$smileys_short_array[$i][1]."' alt='' />";
			}
			?>
		</th>
	</tr>
	
	<tr>
		<th style="width: 300px">
			<?php
			echo "<input type='radio' name='smilechoice' value='noktahhitam'";
			if($choice == "noktahhitam")
			{
				echo " checked='checked'";
			}
			echo " />&nbsp;&nbsp;";
			?><a href='http://www.noktahhitam.com/emoticon-pack-for-wordpress.html'>Noktahhitam</a>&nbsp;-&nbsp;By&nbsp;<a href='http://www.noktahhitam.com'>Noktah&nbsp;Hitam</a>
		</th>
			<th colspan="2">
				<?php
				for($i=0; $i<sizeof($smileys_short_array); $i++)
				{
					echo "<img src='". $path. "noktahhitam/" .$smileys_short_array[$i][1]."' alt='' />";
				}
				?>
			</th>
		</tr>
	</table>
	</div>
	
	<div class="wrap">
	<h3>Other options</h3>
	<p>Here you can choose where to turn your smileys on. If they are not enabled it will show the smileys as text.</p>
	<table class="form-table">
	
	<tr>
		<th scope='row' style="width: 50%;">Smileys enabled / disabled with...</th>
		<td><input type='checkbox' name="enableposts"<?php if($enableposts == "on" || $enableposts == "") { echo 'checked="checked"'; } ?> /> posts / pages<br />
		<input type='checkbox' name="enablecomments"<?php if($enablecomments == "on" || $enablecomments == "") { echo 'checked="checked"'; } ?> /> comments</td>
	</tr>
	
</table>
<p class="submit"><input type="submit" name="submitter" value="Save changes" /></p>
</form>
</div>
<?php
}

function wp_smiley_switcher($content)
{
	$settings = get_option('wp_smiley_switcher');
	$choice = $settings["choice"];
	$dir = $settings["dir"];
		
	$img_start = '<img src="' . get_bloginfo('wpurl') . "/wp-content/plugins/wp-smiley-switcher/";
	if($choice == "yellowpack" || $choice =="")
	{
		$img_start .= "yellowpack";
	}
	elseif($choice == "noktahhitam")
	{
		$img_start .= "noktahhitam";
	}
	else
	{
		$img_start .= $dir;
	}
	
	$img_start .= "/";
	
	$img_end = '" alt="" />';
	$smileys_array = get_array();
			 
	for($i=0; $i<sizeof($smileys_array); $i++)
	{
		$content = str_replace($smileys_array[$i][0],($img_start.$smileys_array[$i][1]. '" title="'.$smileys_array[$i][0].'" />'),$content);
	}
	
	//$comment_text= str_replace("\n","<br/>",$comment_text);
	return $content;
}

function wp_smiley_switcher_comments($comment_text)
{
	$settings = get_option('wp_smiley_switcher');
	$choice = $settings["choice"];
	$dir = $settings["dir"] . '/';
	$enablecomments = $settings["enablecomments"];
	$imgSrc= get_bloginfo('wpurl') . "/wp-content/plugins/wp-smiley-switcher/";
	$imgSrc .= ($choice == "yellowpack" || $choice =="") ? 'yellowpack' : $dir;
	
	$smileys_array = get_array();
			 
	//write the image file name and alt text here
	for($i=0; $i<sizeof($smileys_array); $i++)
	{ 
		$existence = strstr($comment_text, $smileys_array[$i][0]);
		if( strlen($existence) == 0)
			continue;
		$imgURL = "<img src='{$imgSrc}{$smileys_array[$i][1]}' />";
		
		$comment_text = str_replace($existence, $imgURL, $comment_text);
	}
	
	if( function_exists("convertNewlineToBRcode")){
		convertNewlineToBRcode($comment_text);
	}
		
	return $comment_text;
}

function get_array()
{
	$smileys_array = array(
	array(':)', 'icon_smile.gif'),
	array(':-)', 'icon_smile.gif'),
	array(':smile:', 'icon_smile.gif'),
	
	array(':D', 'icon_biggrin.gif'),
	array(':-D', 'icon_biggrin.gif'),
	array(':grin:', 'icon_biggrin.gif'),
	
	array(':(', 'icon_sad.gif'),
	array(':-(', 'icon_sad.gif'),
	array(':sad:', 'icon_sad.gif'),
	
	array(":~(", 'icon_bigsad.gif'),
	array(':cry:', 'icon_bigsad.gif'),
	
	array('8)', 'icon_cool.gif'),
	array('8-)', 'icon_cool.gif'),
	array('B-)', 'icon_cool.gif'),
	array(':cool:', 'icon_cool.gif'),
	
	array(':x', 'icon_blush.gif'),
	array(':-x', 'icon_blush.gif'),
	array(':-B', 'icon_blush.gif'),
	
	array(':P', 'icon_razz.gif'),
	array(':-P', 'icon_razz.gif'),
	array(':razz:', 'icon_razz.gif'),
	
	array(':|', 'icon_neutral.gif'),
	array(':-|', 'icon_neutral.gif'),
	array(':neutral:', 'icon_neutral.gif'),
	
	array(';)', 'icon_wink.gif'),
	array(';-)', 'icon_wink.gif'),
	array(':wink:', 'icon_wink.gif'),
	
	array('=))', 'icon_lol.gif'),
	array(':lol:', 'icon_lol.gif'),
	
	array(':-#', 'icon_redface.gif'),
	array(':oops:', 'icon_redface.gif'),
	
	array(':-@', 'icon_evil.gif'),
	array(':@', 'icon_evil.gif'),
	array(':evil:', 'icon_evil.gif'),
	
	array(':!:', 'icon_exclaim.gif'),
	array(':-!', 'icon_exclaim.gif'),
	array(':!', 'icon_exclaim.gif'),	
	
	
	array(':o', 'icon_surprised.gif'),
	array(':-o', 'icon_surprised.gif'),
	array(':eek:', 'icon_surprised.gif'),
	
	array(':???:', 'icon_confused.gif'),
	array(':-?', 'icon_confused.gif'),
	array(':?', 'icon_confused.gif'),
	
	array('/#)', 'icon_sigh.gif')
	); 
	return $smileys_array;
}

function wp_smiley_switcher_short_array()
{
	return get_array();
}

function wp_smiley_switcher_filters()
{
	$settings = get_option('wp_smiley_switcher');
	$enableposts = $settings["enableposts"];
	$enablecomments = $settings["enablecomments"];
	
	update_option('use_smilies',0);
	
	if($enableposts == "on" || $enableposts == "")
	{
		// new line need not to be replaced by <br/> code in the_content
		add_filter('the_content','wp_smiley_switcher');
	}
	
	if($enablecomments == "on" || $enablecomments == "")
	{
		add_filter('comment_text', 'wp_smiley_switcher_comments');
		add_filter('get_comment_text', 'wp_smiley_switcher_comments');
	}
}

wp_smiley_switcher_filters();
add_action('admin_menu', 'wp_smiley_switcher_submenu');
?>